from flask import Flask,render_template,request
app = Flask(__name__)

@app.route('/')
def index():
    return render_template("login.html")

@app.route('/login',methods=['POST'])
def login():
    # users列表代表已经注册的用户信息
    address_url = "register.html"
    users = [
        {"username":"damu","userid":"2024001","password":"123"},
        {"username":"zdy","userid":"2024002","password":"456"},
        {"username": "hf", "userid": "2024003", "password": "789"},
        {"username": "zk", "userid": "2024004", "password": "000"},
        {"username": "cxy", "userid": "2024005", "password": "666"}
    ]

    #拿到浏览器端传过来的用户id 和 用户密码
    userid = request.form.get("userid")
    password = request.form.get("password")

    #校验用户id 和 用户密码
    usernums = len(users)
    i = 0
    while i < usernums:
        if users[i]["userid"] == userid:
            #有对应的用户 接下来应该查密码
            print(password == users[i]["password"])
            if password == users[i]["password"]:
                address_url = "login_success.html"
            else:
                address_url = "login_error.html"
            break
        i = i + 1

    msg= ""
    username = ""
    if i < usernums:
        username = users[i]["username"]
    if i == usernums:
        msg = "没有此用户,请注册"

    return render_template(address_url,username=username,msg=msg)

@app.route('/register')
def register():
    return render_template("register.html")

if __name__ == '__main__':
    app.run(debug=True)